package com.zxy.supplier_system.server.entity

import jakarta.persistence.CascadeType
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.EntityListeners
import jakarta.persistence.GeneratedValue
import jakarta.persistence.GenerationType
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.ManyToOne
import jakarta.persistence.OneToMany
import jakarta.persistence.OrderBy
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import org.springframework.data.annotation.CreatedBy
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.OffsetDateTime

/**
 * 采购退货
 */
@Entity
@EntityListeners(AuditingEntityListener::class)
@DynamicUpdate
@DynamicInsert
class ProcurementReturn {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id = 0

    @Column(nullable = false)
    lateinit var number: String

    @CreatedBy
    @ManyToOne(optional = false)
    @JoinColumn
    lateinit var createdBy: Employee

    @CreatedDate
    lateinit var createdDateTime: OffsetDateTime

    var submittedDateTime: OffsetDateTime? = null

    @OneToMany(mappedBy = "procurementReturn",cascade = [CascadeType.ALL],orphanRemoval = true)
    @OrderBy(value = "sequence asc")
    var items: MutableList<ProcurementReturnItem> = mutableListOf()

    @ManyToOne(optional = false)
    @JoinColumn
    lateinit var supplier: ProcurementSupplier

}